package com.ruoyi.web.controller.schedule;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.page.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.schedule.domain.Programme;
import com.ruoyi.schedule.service.IProgrammeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 日程信息管理Controller
 * 
 * @author ruoyi
 * @date 2023-03-07
 */
@RestController
@RequestMapping("/schedule/programme")
public class ProgrammeController extends BaseController
{
    @Autowired
    private IProgrammeService programmeService;
   static R r = new R();

    /**
     * 查询日程信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('schedule:programme:list')")
    @GetMapping("/list")
    public TableDataInfo list(Programme programme)
    {
        startPage();
        List<Programme> list = programmeService.selectProgrammeList(programme);
        return getDataTable(list);
    }

    /**
     * 导出日程信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('schedule:programme:export')")
    @Log(title = "日程信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Programme programme)
    {
        List<Programme> list = programmeService.selectProgrammeList(programme);
        ExcelUtil<Programme> util = new ExcelUtil<Programme>(Programme.class);
        util.exportExcel(response, list, "日程信息管理数据");
    }

    /**
     * 获取日程信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('schedule:programme:query')")
    @GetMapping(value = "/{proId}")
    public AjaxResult getInfo(@PathVariable("proId") Integer proId)
    {
        return success(programmeService.selectProgrammeByProId(proId));
    }

    /**
     * 新增日程信息管理
     */
    @PreAuthorize("@ss.hasPermi('schedule:programme:add')")
    @Log(title = "日程信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public boolean add(@RequestBody Programme programme)
    {
        return programmeService.insertProgramme(programme);
    }

    /**
     * 修改日程信息管理
     */
    @PreAuthorize("@ss.hasPermi('schedule:programme:edit')")
    @Log(title = "日程信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public boolean edit(@RequestBody Programme programme)
    {
        return programmeService.updateProgramme(programme);
    }

    /**
     * 删除日程信息管理
     */
    @PreAuthorize("@ss.hasPermi('schedule:programme:remove')")
    @Log(title = "日程信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{proIds}")
    public Boolean remove(@PathVariable Integer[] proIds)
    {
        for (Integer proId : proIds) {
            r.addList(proId);
        }
        return programmeService.deleteProgrammeByProIds(proIds);
    }
    /**
     * 回滚
     */
    @PreAuthorize("@ss.hasPermi('personnel:employee:rollback')")
    @Log(title = "日程信息管理回滚", businessType = BusinessType.DELETE)
    @DeleteMapping
    public Boolean rollback()
    {
        List list = r.getList();

        if (list.size()==0){
            return false;
        }else {
            Object[] array = list.toArray();
            programmeService.rollbackEmployee(array);
            r.setList(new ArrayList<>());
            return true;
        }
    }
}
